package com.study.spmongodb.book.wwj.sp;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Stream;

/**
 * @author MI
 * @version 1.0
 * @date 2020/11/24 23:30
 */
public class CompletableFutureAction3 {
    public static void main(String[] args) {
        final ExecutorService executor = Executors.newFixedThreadPool(2, r -> {
            final Thread thread = new Thread(r);
            thread.setDaemon(false);
            return thread;
        });

      /*  CompletableFuture.supplyAsync(CompletableFutureAction1::get, executor)
              //  .thenApply(CompletableFutureInAction3::multiply)
                .whenComplete((v, t) -> Optional.ofNullable(v).ifPresent(System.out::println));*/

        final List<Integer> productionIDs = Arrays.asList(1, 2, 3, 4, 5);
        final Stream<CompletableFuture<Double>> completableFutureStream = productionIDs.stream().map(i ->
                CompletableFuture.supplyAsync(() -> queryProduct(i), executor)
        );

        // completableFutureStream.map(future -> future.thenApply(future * 2));

        System.out.println("main");
    }

    private static double queryProduct(int i) {
        return CompletableFutureAction1.get();
    }
}
